package com.imti.receiverdemo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity
{

    private DynamicReceiver mReceiver;

    private SecondReceiver mSecond;

    private ThirdReceiver mThird;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView()
    {
        Button btnSendNormal = findViewById(R.id.btn_send_normal);
        Button btnSendOrdered = findViewById(R.id.btn_send_ordered);

        btnSendOrdered.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //构造Intent
                Intent orderedBroadcast = new Intent();
                orderedBroadcast.setAction("com.imti.broadcast.ORDERED");

                //添加附带数据
                orderedBroadcast.putExtra("extra_text",
                    "来自发送广播者的问候");

                //发送有序广播
                sendOrderedBroadcast(orderedBroadcast, null);
            }
        });

        btnSendNormal.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //构造Intent
                Intent normalBroadcast = new Intent();
                normalBroadcast.setAction("com.imti.broadcast.NORMAL");

                //添加附带数据
                normalBroadcast.putExtra("extra_text",
                    "来自发送广播者的问候");

                //发送普通广播
                sendBroadcast(normalBroadcast);
            }
        });
    }

    //动态注册广播接收器，通常在Activity中的onResume()方法中注册
    @Override
    protected void onResume()
    {
        super.onResume();
        mReceiver = new DynamicReceiver();
        //设置广播接收器具体接收哪些广播
        //动态广播接收器可接收几乎所有的广播类型
        IntentFilter intentFilter = new IntentFilter();
        //接收电量变化系统广播
        intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
        intentFilter.addAction("com.imti.broadcast.ORDERED");

        //注册广播接收器
        registerReceiver(mReceiver, intentFilter);

        mSecond = new SecondReceiver();
        mThird = new ThirdReceiver();

        IntentFilter filter = new IntentFilter();
        filter.addAction("com.imti.broadcast.ORDERED");
        filter.setPriority(-100);

        IntentFilter filter2 = new IntentFilter();
        filter2.addAction("com.imti.broadcast.ORDERED");
        filter2.setPriority(100);

        registerReceiver(mSecond, filter);
        registerReceiver(mThird, filter2);
    }
    //动态注册的广播接收器，需要解注册
    @Override
    protected void onPause()
    {
        super.onPause();
        //广播接收器解注册
        unregisterReceiver(mReceiver);

        unregisterReceiver(mSecond);
        unregisterReceiver(mThird);
    }

    class SecondReceiver extends BroadcastReceiver
    {

        private final String TAG = SecondReceiver.class.getSimpleName();

        @Override
        public void onReceive(Context context, Intent intent)
        {
            switch (intent.getAction())
            {
                case "com.imti.broadcast.ORDERED":
                    Log.d(TAG, "Second Receiver接收到：" +
                                   intent.getStringExtra("extra_text"));

                    break;
            }
        }
    }

    class ThirdReceiver extends BroadcastReceiver
    {

        private final String TAG = ThirdReceiver.class.getSimpleName();

        @Override
        public void onReceive(Context context, Intent intent)
        {
            switch (intent.getAction())
            {
                case "com.imti.broadcast.ORDERED":
                    String text = intent.getStringExtra("extra_text");
                    Log.d(TAG, "Thrid Receiver接收到：" + text);

                    text += " 修改";
                    intent.putExtra("extra_text", text);

                    //广播接收器拦截有序广播，优先级较低的接收器不会收到广播
//                    abortBroadcast();
                    break;
            }
        }
    }
}